package com.wgz.mcp.conf;

import com.mysql.cj.jdbc.MysqlDataSource;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DataSourceConfig {

    @Bean
    public HikariDataSource getHikariDataSource() {
        HikariDataSource hikariDataSource = new HikariDataSource();
        MysqlDataSource mysqlDataSource = new MysqlDataSource();

        // 配置 MySQL 数据源
        mysqlDataSource.setURL("jdbc:mysql://127.0.0.1:3306/jsh_erp?useUnicode=true&characterEncoding=utf8&useServerPrepareStmts=false&allowPublicKeyRetrieval=true&useSSL=false");
        mysqlDataSource.setUser("root");
        mysqlDataSource.setPassword("z18965423");

        // 设置到 HikariCP
        hikariDataSource.setDataSource(mysqlDataSource);

        // ========== 连接池核心配置 ==========

        // 连接池大小配置（根据你的应用场景调整）
        hikariDataSource.setMaximumPoolSize(20);          // 最大连接数
        hikariDataSource.setMinimumIdle(5);               // 最小空闲连接数

        // 超时配置
        hikariDataSource.setConnectionTimeout(30000);     // 获取连接超时时间(ms)
        hikariDataSource.setIdleTimeout(600000);          // 连接空闲超时时间(ms)
        hikariDataSource.setMaxLifetime(1800000);         // 连接最大生命周期(ms)
        hikariDataSource.setValidationTimeout(5000);      // 连接验证超时时间(ms)

        // 连接测试和验证
        hikariDataSource.setConnectionTestQuery("SELECT 1");  // 连接测试SQL
        hikariDataSource.setConnectionInitSql("SET NAMES utf8mb4"); // 初始化SQL

        // 连接泄漏检测
        hikariDataSource.setLeakDetectionThreshold(60000); // 连接泄漏检测阈值(ms)

        // 连接池名称（便于监控）
        hikariDataSource.setPoolName("HikariPool-JSH-ERP");

        // ========== MySQL 性能优化配置 ==========

        // 启用预处理语句缓存
        hikariDataSource.addDataSourceProperty("cachePrepStmts", "true");
        hikariDataSource.addDataSourceProperty("prepStmtCacheSize", "250");
        hikariDataSource.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

        // 启用服务器端预处理语句
        hikariDataSource.addDataSourceProperty("useServerPrepStmts", "true");

        // 优化本地会话状态
        hikariDataSource.addDataSourceProperty("useLocalSessionState", "true");
        hikariDataSource.addDataSourceProperty("useLocalTransactionState", "true");

        // 批量操作优化
        hikariDataSource.addDataSourceProperty("rewriteBatchedStatements", "true");

        // 元数据缓存
        hikariDataSource.addDataSourceProperty("cacheResultSetMetadata", "true");
        hikariDataSource.addDataSourceProperty("cacheServerConfiguration", "true");

        // 其他性能优化
        hikariDataSource.addDataSourceProperty("elideSetAutoCommits", "true");
        hikariDataSource.addDataSourceProperty("maintainTimeStats", "false");

        // 字符集和时区设置
        hikariDataSource.addDataSourceProperty("characterEncoding", "utf8");
        hikariDataSource.addDataSourceProperty("useUnicode", "true");
        hikariDataSource.addDataSourceProperty("serverTimezone", "Asia/Shanghai");

        // 连接保持活跃
        hikariDataSource.addDataSourceProperty("tcpKeepAlive", "true");
        hikariDataSource.addDataSourceProperty("tcpNoDelay", "true");

        return hikariDataSource;
    }
}
